class OrderMade < ActiveRecord::Base
  
  validates :order_made_no, :uniqueness => true  
  validates :factory_id,
    :order_made_on,
    :presence =>true  
  validates :order_made_price,
    :numericality => {
      :greater_than => 0
    }
  
  belongs_to :factory
  belongs_to :creator, :class_name => "Employee", :foreign_key => "creator_id"
  has_many :order_made_items
  has_many :feeds, :dependent => :destroy
  
  scope :being_edited, where(:status => 'being_edited')
  scope :sent_to_factory, where(:status => 'sent_to_factory')
  scope :blanked_out, where(:status => 'blanked_out')
  
  before_create do |order_made|
    order_made.init_order_made_no
    order_made.creator = Employee.current
  end
  
  def init_order_made_no
    self.order_made_no ||= "OM#{DateTime.now.to_formatted_s(:number)}"
  end 
  
  def status
    ActiveSupport::StringInquirer.new(self[:status])
  end
  
  def blanked_out!
    update_attribute :status, 'blanked_out'
  end
  
  def being_edited!
    update_attribute :status, 'being_edited'
  end
    
end
